/*
 * ChoosePayWayDialog.java
 * business
 *
 * Created by ChenTao on 2017/2/21.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.yidiandao.business.R;
import com.yidiandao.business.data.bean.PayWay;
import com.yidiandao.business.ui.adapter.PayWayAdapter;
import com.yidiandao.business.ui.widget.listview.DisplayListItem;
import com.yidiandao.business.ui.widget.listview.displaylistitem.PayWayListItem;
import com.yidiandao.business.utils.UIUtils;

import java.util.List;

/**
 * 选择支付方式Dialog
 * <p>
 * <p>Created by ChenTao(chentao7v@gmail.com) on 2017/2/21 14:02
 */
public class ChoosePayWayDialog extends Dialog implements View.OnClickListener {

    private ListView mListView;

    private ImageView mIvClose;

    private Button mBtnFinish;

    private PayWayAdapter mAdapter;

    private OnConfirmPayWayListener mListener;

    private PayWay mPayWay;

    public ChoosePayWayDialog(Context context) {
        super(context, R.style.BottomDialogTheme);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_choose_pay_way);

        initViews();
        initWindow();
        initListView();
        initListeners();
    }

    /**
     * 设置支付方式列表
     */
    public void setPayWays(List<DisplayListItem> payWayListItems) {
        // 最少4个高度
        if (payWayListItems.size() < 4) {
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mListView.getLayoutParams();
            params.height = UIUtils.dp2px(40) * 4;
            mListView.setLayoutParams(params);
        }
        mAdapter.setData(payWayListItems);
    }

    private void initListView() {
        mAdapter = new PayWayAdapter(getContext());
        mListView.setAdapter(mAdapter);
    }

    private void initListeners() {
        mIvClose.setOnClickListener(this);
        mBtnFinish.setOnClickListener(this);
    }

    private void initWindow() {
        // 改变样式-->window的样式
        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        // 设置弹出位置
        params.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
        params.width = WindowManager.LayoutParams.MATCH_PARENT;

        window.setAttributes(params);
    }

    private void initViews() {
        mIvClose = (ImageView) findViewById(R.id.iv_close);
        mBtnFinish = (Button) findViewById(R.id.btn_finish);
        mListView = (ListView) findViewById(R.id.list_view);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_close:
                dismiss();
                break;
            case R.id.btn_finish:
                dismiss();
                if (mListener != null) {
                    mListener.onConfirmPayWay(mPayWay);
                }
                break;
        }
    }

    @Override
    public void show() {
        super.show();

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                // 设置选中项
                PayWay selectedPayWay = ((PayWayListItem) parent.getItemAtPosition(position)).getContentData();

                if (!selectedPayWay.isCanChoose()) {
                    return;
                }

                choosePayWay(selectedPayWay);
                mAdapter.notifyDataSetChanged();
            }
        });
    }

    public void choosePayWay(PayWay selectedPayWay) {

        List<DisplayListItem> payWays = mAdapter.getData();
        for (DisplayListItem payWayItem : payWays) {
            PayWay payWay = ((PayWayListItem) payWayItem).getContentData();
            payWay.setSelected(selectedPayWay.equals(payWay));
        }

        mPayWay = selectedPayWay;
    }

    public void setOnConfirmPayWayListener(OnConfirmPayWayListener listener) {
        mListener = listener;
    }

    public interface OnConfirmPayWayListener {

        void onConfirmPayWay(PayWay payWay);
    }
}
